import 'dart:io';

import 'package:camera/camera.dart';
import 'package:flutter_app/manager/UserInfoManager.dart';
import 'package:flutter_app/network/Loading.dart';
import 'package:flutter_app/ui/home/problem_report/problem_report_view.dart';
import 'package:flutter_app/utils/amap_utils.dart';
import 'package:flutter_app/utils/date_time_utils.dart';
import 'package:flutter_app/utils/permission_utils.dart';
import 'package:flutter_app/utils/sp_utils.dart';
import 'package:flutter_app/utils/video_compress_utils.dart';
import 'package:flutter_app/utils/watermark_image_utils.dart';
import 'package:flutter_app/utils/widget_to_image_utils.dart';
import 'package:flutter_app/values/CommonParam.dart';
import 'package:flutter_app/widget/watermark/watermark_utils.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:get/get_navigation/src/extension_navigation.dart';
import 'package:image_pickers/image_pickers.dart';

import 'camera.dart';
import 'media.dart';

export 'media.dart';

class CameraUtils {

  // 私有构造函数
  CameraUtils._internal() {
    cameras = [];
  }

  // 保存单例
  static final CameraUtils _instance = CameraUtils._internal();

  // 工厂构造函数
  factory CameraUtils() {
    return _instance;
  }

  late List<CameraDescription> cameras;

  // 拍摄照片
  Future<MediaModel?> takePicture({String? dirName}) async {
    // 获取 相机权限
    if (await PermissionUtils().getCamera()) {
      // 获取 麦克风权限
      if (await PermissionUtils().getMicrophone()) {
        // 使用相机
        cameras = await availableCameras();
        MediaModel? mod = await Get.to(() => CameraPage(dirName: dirName,));
        return mod;
      }
    }
    return null;
  }

  // 把拍摄的照片或者视频 带到某个页面中
  Future<void> takePictureToPage({String? dirName}) async {
    // MediaModel? mod = await takePicture();
    // SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
    //     overlays: [SystemUiOverlay.bottom]);
    if (await sp.contains(CommonParam.SP_PROBLEM_REPORT + UserInfoManager().getUserId())) {
      Get.to(() => ProblemReportPage(title: '随手拍问题上报',));
    } else {
      Media? media = await openCameraAddWatermark();
      if (media?.path != null) {
        Get.to(() => ProblemReportPage(title: '随手拍问题上报',), arguments: media);
      }
    }
  }

  // 把照片或者视频 保存到指定文件夹下
  Future<Media?> openCamera({String? dirName}) async {
    MediaModel? mediaModel = await takePicture(dirName: dirName);
    Loading.show();
    if (mediaModel != null) {
      Media media = new Media();
      media.name = mediaModel.file?.name;
      media.path = mediaModel.file?.path;
      if (!mediaModel.isImage) {
        final thumbnailFile = await VideoCompressUtils().getFileThumbnail(mediaModel.file?.path ?? "",);
        media.thumbPath = thumbnailFile.path;
        media.galleryMode = GalleryMode.video;
      } else {
        media.galleryMode = GalleryMode.image;
      }
      Loading.dismiss();
      return media;
    }
    Loading.dismiss();
    return null;
  }

  // 拍照的照片或者视频 添加水印
  Future<Media?> openCameraAddWatermark({String? dirName}) async {

    // 开始 请求权限 获取定位信息，如果没开启定位，则不能进行后续拍照功能
    LocationInfo? locationInfo = await AMapUtils().startLocation();
    if (locationInfo == null) {
      return null;
    }
    // 如果有水印就隐藏
    WatermarkUtils().hide();
    MediaModel? mediaModel = await takePicture(dirName: dirName);
    // 如果有水印就显示
    WatermarkUtils().show();

    Loading.show();
    if (mediaModel != null) {
      // 获取水印文件
      File watermark = await WidgetToImageUtils().widgetToImage(locationInfo.address);

      Media media = new Media();
      media.name = mediaModel.file?.name;
      media.address = locationInfo.address;
      media.longitude = locationInfo.longitude;
      media.latitude = locationInfo.latitude;
      media.dateTime = DateTimeUtils.getNow();
      if (mediaModel.isImage) {
        // 添加水印并覆盖原来的照片
        await WatermarkImageUtils().imageAddImageWaterMark(mediaModel.file!.path, watermark.path);
        media.path = mediaModel.file?.path;
        media.galleryMode = GalleryMode.image;
      } else {
        final thumbnailFile = await VideoCompressUtils().getFileThumbnail(mediaModel.file?.path ?? "",);
        media.thumbPath = thumbnailFile.path;
        // 添加水印并覆盖原来的视频
        media.path = mediaModel.file?.path;
        media.galleryMode = GalleryMode.video;
      }
      Loading.dismiss();
      return media;
    }
    Loading.dismiss();
    return null;
  }
}
